﻿using MediatR;
using W.Common.Enum;
using W.Purchase.Domain;
using W.Purchase.Domain.DTO;
using W.Purchase.Domain.PageDTO;
using W.Purchase.Read.API.AppLication.Command.PayableCommand;
using W.Purchase.Repository.Impl;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.AppLication.CommandHandler.PayableCommandHandler
{
    public class PayableShowCommandHandler:IRequestHandler<PayableShowCommand,Pages<PayableShowDto>>
    {
        private readonly IPayableRepository _ipayableRepository;
        public PayableShowCommandHandler(IPayableRepository ipayableRepository)
        {
            _ipayableRepository = ipayableRepository;
        }

        public async Task<Pages<PayableShowDto>> Handle(PayableShowCommand request, CancellationToken cancellationToken)
        {
            Pages<PayableShowDto> page = new Pages<PayableShowDto>();
            var list = from a in await _ipayableRepository.GetAllAsaync()
                       where a.IsDelete == false
                       select new PayableShowDto
                       {
                           PayableId = a.PayableId,
                           PayableSerialNumber = a.PayableSerialNumber,
                           PayableName = a.PayableName,
                           FinanceTypeName = a.FinanceTypeName,
                           EndDate = a.EndDate,
                           Payer = a.Payer,
                           Payee = a.Payee,
                           PayableMoney = a.PayableMoney,
                           ActuallyPaidMoney = a.ActuallyPaidMoney,
                           ResidueMoney = a.ResidueMoney,
                           Status = a.Status,
                           StatusText = a.Status.ToString(),
                           PaymentNum = a.PaymentNum,
                           Receivableremark = a.Receivableremark,
                           office_id = a.office_id,
                           user_id = a.user_id,
                           CreateTime = a.CreateTime,
                           IsDelete = a.IsDelete,
                       };
            if (!string.IsNullOrEmpty(request.PayableSerialNumber))
            {
                list = list.Where(c => c.PayableSerialNumber == request.PayableSerialNumber).ToList();
            }
            if (!string.IsNullOrEmpty(request.PayableName))
            {
                list = list.Where(c => c.PayableName.Contains(request.PayableName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.FinanceTypeName))
            {
                list = list.Where(c => c.FinanceTypeName == request.FinanceTypeName).ToList();
            }
            if (!string.IsNullOrEmpty(request.Payee))
            {
                list = list.Where(c => c.Payee.Contains(request.Payee)).ToList();
            }
            if (request.StartTime != null)
            {
                list = list.Where(c => c.CreateTime >= request.StartTime).ToList();
            }
            if (request.EndTime != null)
            {
                list = list.Where(c => c.CreateTime <= request.EndTime).ToList();
            }
            if (request.Status > 0)
            {
                list = list.Where(c => c.Status == (ReceivableStatus)request.Status).ToList();
            }
            page.TotalCount = list.Count();
            page.TotalPage = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.List = list.OrderByDescending(c => c.PayableId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;
        }
    }
}
